package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.shycloud.common.minio.service.MinioTemplate;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.BasicAppShoppingItem;
import com.shycloud.mido.member.api.entity.BasicShoppingBanner;
import com.shycloud.mido.member.api.vo.ShoppingRecommendGoodsVO;
import com.shycloud.mido.member.mapper.BasicShoppingItemMapper;
import com.shycloud.mido.member.service.BasicShoppingBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@AllArgsConstructor
@RequestMapping("/shopping/banner")
@Api(value = "后台商城 banner 设置接口", tags = "后台商城 banner 设置接口")
@CrossOrigin(origins = "*")@ApiIgnore
public class ShoppingBannerController {

	/** 商城 banner业务 service */
	private BasicShoppingBannerService shoppingBannerService;
	/** 商城热门推荐业务 mapper */
	private BasicShoppingItemMapper shoppingItemMapper;

	private MinioTemplate minioTemplate;

	@ApiOperation("Banner管理 - 添加 banner")
	@ApiOperationSupport(order = 2)
	@PostMapping("addBanner")
	@ApiImplicitParam(name = "Authorization", value = "请求头认证参数 示例值（注意中间有个空格）：Bearer 登录token", paramType = "header", dataType = "string", required = true)
	public R<Boolean> addBanner(@RequestBody BasicShoppingBanner banner) {
		banner.setCreateTime(new Date());
		return new R(shoppingBannerService.save(banner));
	}

	@ApiOperation(value = "Banner管理 - 编辑 banner")
	@ApiOperationSupport(order = 3)
	@PostMapping("updBanner")
	public R<Boolean> updBanner(@RequestBody BasicShoppingBanner banner) {
		return new R(shoppingBannerService.updateById(banner));
	}

	@ApiOperation(value = "Banner管理 - 删除 Banner 图片", notes = "只传ID即可")
	@PostMapping("delBannerImage")
	@ApiOperationSupport(order = 4)
	public R<Boolean> delBannerImage(@RequestBody BasicShoppingBanner banner) {
		banner = shoppingBannerService.getById(banner.getId());
		banner.setImgUrl("");
		return new R(shoppingBannerService.updateById(banner));
	}

	@ApiOperation(value = "Banner管理 - 删除 Banner", notes = "只传ID即可")
	@PostMapping("delBanner")
	@ApiOperationSupport(order = 5)
	public R<Boolean> delBanner(@RequestBody BasicShoppingBanner banner) {
		return new R(shoppingBannerService.removeById(banner.getId()));
	}

	@ApiOperation("热门推荐 - 根据ID搜索商品信息")
	@GetMapping("getGoodsById")
	@ApiOperationSupport(order = 7, author = "nianhua.jiang")
	@ApiImplicitParam(name = "id", value = "商品ID", paramType = "query", dataType = "int", required = true)
	public R<ShoppingRecommendGoodsVO> getGoodsById(Integer id) {
		return new R(shoppingBannerService.getGoodsById(id));
	}

	@ApiOperation(value = "热门推荐 - 添加推荐商品", notes = "只传 goodsId 和 unitPrice")
	@ApiOperationSupport(order = 8)
	@PostMapping("addRecommend")
	public R<Boolean> addRecommend(@RequestBody BasicAppShoppingItem item) {

		//判断是否大于四条
		if (shoppingItemMapper.checkNumGoods() >= 4) {
			return new R<Boolean>(CommonConstants.FAIL, "最多只能添加4个热门推荐", false);
		}
		if (item.getGoodsId() == null && item.getCollectionId() == null) {
			return new R<Boolean>(CommonConstants.FAIL, "丢失曲集ID或者商品ID", false);
		}

		//查重校验
		List<BasicAppShoppingItem> goodsItem = shoppingItemMapper.selectList(new QueryWrapper<BasicAppShoppingItem>()
				.eq("goods_id", item.getGoodsId()).or().eq("goods_id", item.getCollectionId()));

		if (goodsItem.size() > 0) {
			return new R<Boolean>(CommonConstants.FAIL, "此热门推荐已在列表中，请重新添加", false);
		}

		item.setRecommendStatus(1);

		return new R<Boolean>(shoppingItemMapper.insert(item) == 1);
	}

	@ApiOperation(value = "热门推荐 - 删除热门推荐", notes = "只传ID即可")
	@PostMapping("delRecommend")
	@ApiOperationSupport(order = 9)
	public R<Boolean> delRecommend(@RequestBody BasicAppShoppingItem item) {
		return new R(shoppingItemMapper.deleteById(item.getId()));
	}

	@ApiOperation("谱库大全 - 根据曲集ID搜索曲集信息")
	@GetMapping("getCollectionById")
	@ApiImplicitParam(name = "id", value = "曲集ID", paramType = "query", dataType = "int", required = true)
	public R<ShoppingRecommendGoodsVO> getCollectionById(Integer id) {
		return new R(shoppingBannerService.getCollectionById(id));
	}

	@ApiOperation(value = "谱库大全 - 添加曲集", notes = "只传 collectionId 和 unitGoldPrice")
	@PostMapping("addCollection")
	public R addCollection(@RequestBody BasicAppShoppingItem item) {

		if (item.getUnitGoldPrice() == 0) {
			return new R<Boolean>(CommonConstants.FAIL, "曲集价格为0，无法添加", false);
		}

		//判断是否大于四条
		if (shoppingItemMapper.checkNumCollection() >= 4) {
			return new R<Boolean>(CommonConstants.FAIL, "最多只能添加4本推荐曲集", false);
		}

		//查重校验
		List<BasicAppShoppingItem> goodsItem = shoppingItemMapper.selectList(new QueryWrapper<BasicAppShoppingItem>()
				.eq("collection_id", item.getCollectionId())
				.eq("recommend_coll", 1));

		if (goodsItem.size() > 0) {
			return new R<Boolean>(CommonConstants.FAIL, "此曲集已在列表中，请重新添加", false);
		}

		item.setRecommendColl(1);
		return new R<Boolean>(shoppingItemMapper.insert(item) == 1);
	}

	/**
	 * 上传 Banners， 前端需要用 form 传输
	 *
	 * @return
	 */
	@PostMapping("/uploadBanners")
	@ApiOperation("Banner管理 - 上传Banner")
	@ApiImplicitParam(name = "file", value = "文件", paramType = "form", dataType = "MultipartFile", required = true)
	public R batchUploadBanners(@RequestParam("file") MultipartFile file) throws IOException {

		if (file == null) {
			throw new BusinessException("请选择上传图片!");
		}

		String fileName = file.getOriginalFilename().replace(" ", "");
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);

		String uuid = UUID.randomUUID().toString();
		//提取图片名称
		String imgName = uuid.replace("-", "") + "." + suffix;

		return new R(minioTemplate.uploadFileWX(file.getInputStream(), imgName, "zebra-banner","http://banner.joymido.com"));
	}

}
